<?php
namespace api\know\controller;

use api\know\model\MaterialModel;
use api\know\model\TeachingMaterialContentModel;
use api\know\model\ResourceModel;
use api\know\model\QuestionModel;
use api\know\model\CollectModel;
use api\know\controller\KnowRestUserController;
use think\facade\Db;

class CollectController extends KnowRestUserController
{
    public function add()
    {
        $uuid = $this->request->param('uuid', '');
		$type = $this->request->param('type');
		if (empty($uuid))
        {
            $this->error('收藏id不能为空');
        }
		else if (empty($type))
        {
            $this->error('类型不能为空');
        }

        $c_wh[] = ['type', '=', $type];
        $c_wh[] = ['relate_uuid', '=', $uuid];

        $c = CollectModel::where($c_wh)->field(CollectModel::$QueryField)->find();
		if (!empty($c)) 
		{
			$this->success('保存成功');
		}
        
        $uid = $this->getUserId('uid');
		$c_u = [
			'uid' => $uid,
			'relate_uuid' => $uuid,
			'type' => $type,
			'create_user' => $this->user['user_nickname'],
		];
		$row = CollectModel::create($c_u);
        $this->success('收藏成功', $row);
    }

    public function cancel()
    {
        $id = $this->request->param('id', '');
		if (empty($id))
        {
            $this->error('收藏id不能为空');
        }

        $c_wh[] = ['id', '=', $id];
        CollectModel::where($c_wh)->field(CollectModel::$QueryField)->delete();
		
        $this->success('已取消收藏');
    }

	public function get()
    {
        $uuid = $this->request->param('uuid', '');
		$type = $this->request->param('type');
		if (empty($uuid))
        {
            $this->error('收藏id不能为空');
        }
		else if (empty($type))
        {
            $this->error('类型不能为空');
        }

        $c_wh[] = ['type', '=', $type];
        $c_wh[] = ['relate_uuid', '=', $uuid];
        $c = CollectModel::where($c_wh)->field(CollectModel::$QueryField)->find();
	
        $this->success('保存成功', $c);
    }



}
